package com.oschina.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author shihai.liu
 * @ClassName: DateUtil
 * @Description: 时间工具
 * @date 2014年6月4日 下午5:02:20
 */
public class DateUtil {

	private static ThreadLocal<DateFormat> fullDateFormatter = new ThreadLocal<DateFormat>();
	private static ThreadLocal<DateFormat> shortDateFormatter = new ThreadLocal<DateFormat>();
	private static ThreadLocal<DateFormat> dateFormatter = new ThreadLocal<DateFormat>();
	private static ThreadLocal<DateFormat> yearFormatter = new ThreadLocal<DateFormat>();
	private static ThreadLocal<DateFormat> uploadFormatter = new ThreadLocal<DateFormat>();

	/**
	 * @param date
	 * @return String
	 * @throws
	 * @Title: dateToMin
	 * @Description: 转化到分
	 */
	public static String dateToMin(final Date date) {
		DateFormat df = fullDateFormatter.get();
		if (df == null) {
			df = new SimpleDateFormat("yyyyMMddHHmm");
			fullDateFormatter.set(df);
		}
		return df.format(date);
	}

	/**
	 * @param date
	 * @return String
	 * @throws
	 * @Title: dateToHou
	 * @Description: 转化到时
	 */
	public static String dateToHou(final Date date) {
		DateFormat df = shortDateFormatter.get();
		if (df == null) {
			df = new SimpleDateFormat("yyyyMMddHH");
			shortDateFormatter.set(df);
		}
		return df.format(date);
	}

	/**
	 * @param date
	 * @return String
	 * @throws
	 * @Title: dateToDay
	 * @Description: 转化到天
	 */
	public static String dateToDay(final Date date) {
		DateFormat df = dateFormatter.get();
		if (df == null) {
			df = new SimpleDateFormat("yyyyMMdd");
			dateFormatter.set(df);
		}
		return df.format(date);
	}

	/**
	 * @param date
	 * @return String
	 * @throws
	 * @Title: dateToYear
	 * @Description: 转化到年
	 */
	public static String dateToYear(final Date date) {
		DateFormat df = yearFormatter.get();
		if (df == null) {
			df = new SimpleDateFormat("yyyy");
			yearFormatter.set(df);
		}
		return df.format(date);
	}

	/**
	 * @return Integer
	 * @throws
	 * @Title: getYear
	 * @Description: 获取年，公元纪年
	 */
	public static Integer getYear() {
		return Integer.parseInt(DateUtil.dateToYear(new Date()));
	}

	public static String getFilePath() {
		DateFormat df = uploadFormatter.get();
		if (df == null) {
			df = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss/");
			uploadFormatter.set(df);
		}
		return df.format(new Date());
	}

	public static Date date2Str(String Str) {
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(Str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	// hxn:jiangxi ----------------------------------------------------

	/**
	 * -1:昨天 0:表示当天 1:明天 以此类推
	 *
	 * @param day
	 * @return
	 */
	public static JSONObject formatDay(Integer day) {
		JSONObject json = new JSONObject();
		Date date = new Date();
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, day);
		date = calendar.getTime();
		DateFormat dayF = new SimpleDateFormat("M月d日");
		DateFormat weekF = new SimpleDateFormat("E", Locale.CHINA);
		json.put("day", dayF.format(date));
		json.put("week", weekF.format(date));
		return json;
	}

	public static String getNowtime() {
		SimpleDateFormat formatter = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
		String str = formatter.format(curDate);
		return str;
	}

	public static String getNowDate() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
				Locale.CHINA);
		// sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
		String str = formatter.format(curDate);
		return str;
	}

	public static String formatTo(String dates) {
		try {
			DateFormat df = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
			Date date1 = df.parse(dates);
			DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
			return df2.format(date1);
		} catch (Exception e) {
			return null;
		}

	}

	public static long twoTimeDiffer(String strBegin, String strEnd) {
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINA);
		long between = 0;
		try {
			Date begin = dfs.parse(strBegin);
			Date end = dfs.parse(strEnd);
			between = (end.getTime() - begin.getTime());// 得到两者的毫秒数
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return between;
	}


	public static String formatTimeYyMmDdHhMmSs(Date nowTime) {
		SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINA);
		return time.format(nowTime);
	}

	/**
	 * 获取3分钟后的时间
	 *
	 * @return 3分钟后的时间
	 */
	public static String getAfterMinuteDate(long l) {
		Date nowDate = new Date();
		Date afterDate = new Date(nowDate.getTime() + l);

		SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINA);

		return time.format(afterDate);

	}

	@SuppressWarnings("deprecation")
	public static List<String> dateToWeek() {
		Date currentDate = new Date();
		// 定义输出日期格式
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd", Locale.CHINA);
		// int b = currentDate.getDay();
		// 星期日获取的数据为0 需要转换一下
		int b = currentDate.getDay() == 0 ? 7 : currentDate.getDay();
		Date fdate;
		List<String> list = new ArrayList<String>();
		Long fTime = currentDate.getTime() - b * 24 * 3600000;
		for (int a = 1; a <= 7; a++) {
			fdate = new Date();
			fdate.setTime(fTime + (a * 24 * 3600000));
			list.add(a - 1, sdf.format(fdate));
		}
		return list;
	}

	@SuppressWarnings("deprecation")
	public static List<String> date2Week() {
		Date currentDate = new Date();
		// 定义输出日期格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
		// 星期日获取的数据为0 需要转换一下
		int b = currentDate.getDay() == 0 ? 7 : currentDate.getDay();
		Date fdate;
		List<String> list = new ArrayList<String>();
		Long fTime = currentDate.getTime() - b * 24 * 3600000;
		for (int a = 1; a <= 7; a++) {
			fdate = new Date();
			fdate.setTime(fTime + (a * 24 * 3600000));
			list.add(a - 1, sdf.format(fdate));
		}
		return list;
	}

	public static String getCurrentDate() {
		// 定义输出日期格式
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd", Locale.CHINA);
		Date currentDate = new Date();
		return sdf.format(currentDate);
	}

	public static String getCurrentDateTime() {
		// 定义输出日期格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINA);
		Date currentDate = new Date();
		return sdf.format(currentDate);
	}

	/**
	 * 获取当日 前后时间
	 *
	 * @param delta_T
	 *            时间差
	 * @return
	 */
	public static String getDateYyMmDd(int delta_T) {
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, delta_T);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
				Locale.CHINA);
		String dateString = formatter.format(date);

		return dateString;
	}

	// string类型转换为date类型
	// strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
	// HH时mm分ss秒，
	// strTime的时间格式必须要与formatType的时间格式相同
	public static Date stringToDate(String strTime, String formatType)
			throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat(formatType,
				Locale.CHINA);
		Date date = null;
		date = formatter.parse(strTime);
		return date;
	}

	// string类型转换为long类型
	// strTime要转换的String类型的时间
	// formatType时间格式
	// strTime的时间格式和formatType的时间格式必须相同
	public static long stringToLong(String strTime, String formatType)
			throws ParseException {
		Date date = stringToDate(strTime, formatType); // String类型转成date类型
		if (date == null) {
			return 0;
		} else {
			long currentTime = dateToLong(date); // date类型转成long类型
			return currentTime;
		}
	}

	// date类型转换为String类型
	// dateTime要转换的String类型的时间
	// formatType时间格式
	// dateTime的时间格式和formatType的时间格式必须相同
	public static String dateToString(Date dateTime, String formatType)
			throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat(formatType,
				Locale.CHINA);
		String dateStr = formatter.format(dateTime);
		return dateStr;
	}

	// date类型转换为long类型
	// date要转换的date类型的时间
	public static long dateToLong(Date date) {
		return date.getTime();
	}



	/**
	 * @param dt
	 *            当前日期
	 * @return
	 * @Title: DateUtils
	 * @author:郭松胜
	 * @Description: TODO 获取当前日期dt对应周几
	 */
	public static String getDateWithWeek(Date dt) {
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	public static String Timestamp2Time(Long time) {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("HHmm",
					Locale.CHINA);
			Timestamp date = new Timestamp(time);
			return dateFormat.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String Timestamp2ShowTime(Long time) {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm",
					Locale.CHINA);
			Timestamp date = new Timestamp(time);
			return dateFormat.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	/**
	 * 取得上一天 2014-05-21 >> 2014-05-20
	 *
	 * @param date
	 * @return
	 */
	public static String getLastDay(String date, String offset) {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date et = ymd.parse(date);
			c.setTime(et);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if (StringUtils.isEmpty(offset)) {
			c.add(Calendar.DAY_OF_YEAR, -1);
		} else {
			c.add(Calendar.DAY_OF_YEAR, -(Integer.parseInt(offset)));
		}
		return ymd.format(c.getTime());
	}

	public static void main(String[] args) {
		System.out.println(DateUtil.date2Str("2018-12-31 23:59:59"));
	}
}
